home *** CD-ROM | disk | FTP | other *** search
/ PC Player 2004 May / pc player 2004-05.iso / Demos / FarCry / Data1.cab / _0FCF609357F040EE8D10436FDBB74592 < prev    next >
Encoding:
Text File  |  2004-01-06  |  15.4 KB  |  479 lines

  1. AutomaticDoor = {
  2.     Properties = {
  3.           Direction = {
  4.                 X=1,
  5.               Y=0,
  6.                 Z=0,
  7.           },
  8.           CloseDelay=1.5,
  9.           BBOX_Size={
  10.                 X=3,
  11.               Y=3,
  12.                 Z=2,
  13.           },
  14.           MovingDistance = 0.9,
  15.           MovingSpeed = 2,
  16.           bPlayerBounce = 1,
  17.           bPlayerOnly = 0,
  18.         fileAnimatedModel= "MISSING.cga",
  19.           fileModel_Left = "Objects/glm/ww2_indust_set1/doors/door_indust_2_left.cgf",
  20.           fileModel_Right = "Objects/glm/ww2_indust_set1/doors/door_indust_2_right.cgf",
  21.           fileOpenSound = "Sounds/doors/open.wav",
  22.           fileCloseSound = "Sounds/doors/close.wav",
  23.           bAutomatic = 1,
  24.           bCloseTimer = 1,
  25.           bEnabled = 1,
  26.           iNeededKey=-1,
  27.         bUseAnimatedModel=0,
  28.         bUsePortal=1,
  29.         TextInstruction=Localize("NeedKey"),
  30.       },
  31.   CurrModel = "Objects/lift/lift.cgf",
  32.   temp_vec={x=0,y=0,z=0},
  33.   Distance = 0,
  34.   EndReached=nil,
  35.   OpeningTime=0,
  36.   bLocked=false,
  37.   bInitialized=nil,
  38. }
  39.  
  40. function AutomaticDoor:OnPropertyChange()
  41.     self:OnReset();
  42.     if ( self.Properties.fileOpenSound ~= self.CurrOpenSound ) then
  43.         self.CurrOpenSound=self.Properties.fileOpenSound;
  44.         self.OpenSound=Sound:Load3DSound(self.CurrOpenSound);
  45.     end
  46.     if ( self.Properties.fileCloseSound ~= self.CurrCloseSound ) then
  47.         self.CurrCloseSound=self.Properties.fileCloseSound;
  48.         self.CloseSound=Sound:Load3DSound(self.CurrCloseSound);
  49.     end
  50. end
  51.  
  52. function AutomaticDoor:OnReset()
  53.     if (self.Properties.bUseAnimatedModel==0) then
  54.         if (self.Properties.fileModel_Right ~= "") then
  55.             self:LoadObject( self.Properties.fileModel_Right, 0, 0 );
  56.         end
  57.         if (self.Properties.fileModel_Left ~= "") then
  58.             self:LoadObject( self.Properties.fileModel_Left, 1, 0 );
  59.         end
  60.         self:DrawObject( 0, 1 );
  61.         self:DrawObject( 1, 1 );
  62.         self:CreateStaticEntity( 100,-1 ); -- -1 is surface_idx, which means 'not used'
  63.         --self:CreateStaticEntity( 100, 1 );
  64.     else        
  65.         self:LoadCharacter(self.Properties.fileAnimatedModel, 0 ,0);
  66.         self:DrawObject( 0, 1 );
  67.     end
  68.  
  69.     self.CurrOpenSound=self.Properties.fileOpenSound;
  70.     self.OpenSound=Sound:Load3DSound(self.CurrOpenSound);
  71.     self.CurrCloseSound=self.Properties.fileCloseSound;
  72.     self.CloseSound=Sound:Load3DSound(self.CurrCloseSound);
  73.     self:SetBBox({x=-(self.Properties.BBOX_Size.X*0.5),y=-(self.Properties.BBOX_Size.Y*0.5),z=-(self.Properties.BBOX_Size.Z*0.5)},
  74.     {x=(self.Properties.BBOX_Size.X*0.5),y=(self.Properties.BBOX_Size.Y*0.5),z=(self.Properties.BBOX_Size.Z*0.5)});
  75.     self:RegisterState("Inactive");
  76.     self:RegisterState("Opened");
  77.     self:RegisterState("Closed");
  78.  
  79.     if (self.Properties.iNeededKey~=-1) then
  80.         --System:LogToConsole("Need the key!");
  81.         self.bLocked=1;
  82.     else
  83.         self.bLocked=0;
  84.     end
  85.  
  86.     if(self.Properties.bEnabled==1)then
  87.         self:GotoState( "Closed" );
  88.     else
  89.         self:GotoState( "Inactive" );
  90.     end
  91.  
  92.     if(self.MovingSpeed==0)then
  93.         self.MovingSpeed=0.01;
  94.     end
  95.     --calculate how long the door to be opened
  96.     self.OpeningTime=self.Properties.MovingDistance/self.Properties.MovingSpeed;
  97.     self.Timer=0;
  98.     
  99.     self:UpdatePortal();
  100. end
  101.  
  102.  
  103. function AutomaticDoor:OnSave(stm)
  104.     stm:WriteInt(self.bLocked);
  105. end
  106.  
  107.  
  108. function AutomaticDoor:OnLoad(stm)
  109.     self.bLocked = stm:ReadInt();
  110. end
  111.  
  112. function AutomaticDoor:OnInit()
  113.     self:EnableUpdate(0);
  114.     self:TrackColliders(1);
  115.  
  116.     if(self.bInitialized==nil)then
  117.         self:OnReset();
  118.         self.bInitialized=1;
  119.     end
  120. end
  121.  
  122. function AutomaticDoor:Event_Open(sender)
  123.     self:GotoState( "Opened" );
  124.     BroadcastEvent(self, "Open");
  125.     self:UpdatePortal();
  126.     if (self.Properties.bUseAnimatedModel==1) then
  127.         self:StartAnimation(0,self.Properties.Animation);
  128.     end
  129. end
  130.  
  131. function AutomaticDoor:Event_Activate(sender)
  132.     self:GotoState( "Closed" );
  133.     BroadcastEvent(self, "Activate");
  134.     --System:LogToConsole("Door Active");
  135. end
  136.  
  137. function AutomaticDoor:Event_Deactivate(sender)
  138.     self:GotoState( "Inactive" );
  139.     BroadcastEvent(self, "Deactivate");
  140.     --System:LogToConsole("Door Inactive");
  141. end
  142.  
  143. function AutomaticDoor:Event_Close(sender)
  144.     self:GotoState( "Closed" );
  145.     BroadcastEvent(self, "Close");
  146.     if (self.Properties.bUseAnimatedModel==1) then
  147.         self:StartAnimation(0,self.Properties.Animation,0,1.5,-1);
  148.     end
  149. end
  150.  
  151. function AutomaticDoor:Event_Opened(sender)
  152.     self:EnableUpdate(0);
  153.     BroadcastEvent(self, "Opened");
  154. end
  155.  
  156. ----------------------------------------------------------
  157. function AutomaticDoor:Event_Closed(sender)
  158.     self:EnableUpdate(0);
  159.     BroadcastEvent(self, "Closed");
  160.     self:UpdatePortal();
  161. end
  162.  
  163. ----------------------------------------------------------
  164. function AutomaticDoor:Event_Unlocked(sender)
  165.     self.bLocked=0;
  166.     BroadcastEvent(self, "Unlocked");
  167. end
  168.  
  169. ----------------------------------------------------------
  170. function AutomaticDoor:Event_ForceClose(sender)
  171.     self.bLocked=1;
  172.     self:GotoState( "Closed" );
  173.     self:Event_Close(self);
  174. end
  175.  
  176. ----------------------------------------------------------
  177. --OnOpen = function (self,other)
  178. function AutomaticDoor:OnOpen(self,other,usepressed)
  179.  
  180.     if ((not other.cnt)) then
  181.         return 0
  182.     end
  183.  
  184.  
  185.     -- if the door is locked we need to check for the key first
  186.     if (self.bLocked~=0) then
  187.  
  188.         if (other.keycards and (other.keycards[self.Properties.iNeededKey]==1)) then
  189.             other.keycards[self.Properties.iNeededKey]=0;
  190.             self:Event_Unlocked(self);                
  191.  
  192.             System:LogToConsole("Door     unlocked !");
  193.         else
  194.             --System:LogToConsole("Key not available !");                                                
  195.  
  196.             -- this message should be shown to the local player only
  197.             if ((other==_localplayer) and self.Properties.iNeededKey>=0) then
  198.                 Hud.label = self.Properties.TextInstruction;
  199.             end
  200.             self:SetTimer(100);
  201.             return 0
  202.         end
  203.  
  204.     end
  205.  
  206.     ------
  207.     if(self.Properties.bPlayerOnly==1 and (other.type~="Player"))then
  208.         return
  209.     end    
  210.  
  211.     if ((self.Properties.bAutomatic==1) or ((self.Properties.bAutomatic==0) and (usepressed==1)) and (self.EndReached==nil)) then
  212.         self:Event_Open(self);
  213.         return 1
  214.     end            
  215.  
  216.     return 0
  217. end
  218.  
  219.  
  220.  
  221. function AutomaticDoor:OnUse(player)
  222.     if ( self.Properties.bAutomatic==0 and (self.Properties.bEnabled==1) and (self.bLocked==0)) then
  223.         return (self:OnOpen(self,player,1));
  224.     end
  225.  
  226.     return 0
  227. end
  228.  
  229.  
  230. function AutomaticDoor:UpdatePortal()
  231.     if (self.Properties.bUsePortal==1) then
  232.         local bOpen = self:GetState() == "Opened";    
  233.     
  234.         System:ActivatePortal(self:GetPos(),bOpen,self.id);
  235.     end
  236. end
  237.  
  238.  
  239. ------------------------------------------------------------------------------------------
  240. ------------------------------------------------------------------------------------------
  241. -- SERVER
  242. ------------------------------------------------------------------------------------------
  243. ------------------------------------------------------------------------------------------
  244.  
  245. AutomaticDoor["Server"]={
  246.     OnInit = AutomaticDoor.OnInit,
  247.     ------------------------------------------------------------------------------------------
  248.     --INACTIVE
  249.     ------------------------------------------------------------------------------------------
  250.     Inactive = {
  251.     },
  252.     ------------------------------------------------------------------------------------------
  253.     --OPENED
  254.     ------------------------------------------------------------------------------------------
  255.     Opened = {
  256.         -- Called when Opened State is Set.
  257.     ----------------------------------------------------------
  258.         OnBeginState = function(self)
  259.             --System.LogToConsole("SERVER:Opened");
  260.             if(self.Properties.bCloseTimer==1)then
  261.                 self:SetTimer(self.Properties.CloseDelay*1000);
  262.             end
  263.             if(self.Timer~=0)then
  264.                 Game:KillTimer(self.Timer);
  265.             end
  266.             self.Timer=Game:SetTimer(self,self.OpeningTime);
  267.             self.EventSent = nil;
  268.         end,
  269.     ----------------------------------------------------------
  270.         OnEndState = function(self)
  271.             Game:KillTimer(self.Timer);
  272.             self.Timer=0;
  273.         end,
  274.     ----------------------------------------------------------
  275.         OnContact = function(self,other)
  276.             --System:LogToConsole("SERVER:OnContact");
  277.             if(self.Properties.bCloseTimer==1)then
  278.                 if(self.Properties.bPlayerOnly==1 and (other.type~="Player"))then
  279.                     return
  280.                 end
  281.                 self:SetTimer(self.Properties.CloseDelay*1000);
  282.             end
  283.         end,
  284.     ----------------------------------------------------------
  285.         OnTimer = function(self)
  286.             --System:LogToConsole("Opened State Timer Exprired");
  287.             -- close door.
  288.             self:GotoState( "Closed" );
  289.             self:Event_Close(self);
  290.         end,
  291.     ----------------------------------------------------------
  292.         OnEvent = function(self,eid)
  293.             if ((eid==ScriptEvent_OnTimer) and (self.Properties.bUseAnimatedModel==0)) then
  294.                 self.temp_vec.x = self.Properties.Direction.X * self.Properties.MovingDistance;
  295.                 self.temp_vec.y = self.Properties.Direction.Y * self.Properties.MovingDistance;
  296.                 self.temp_vec.z = self.Properties.Direction.Z * self.Properties.MovingDistance;
  297.                 self:SetObjectPos(0,self.temp_vec);
  298.                 self.temp_vec.x =-(self.Properties.Direction.X * self.Properties.MovingDistance);
  299.                 self.temp_vec.y =-(self.Properties.Direction.Y * self.Properties.MovingDistance);
  300.                 self.temp_vec.z =-(self.Properties.Direction.Z * self.Properties.MovingDistance);
  301.                 self:SetObjectPos(1,self.temp_vec);
  302.             end
  303.         end
  304.     ----------------------------------------------------------
  305.     },
  306.     ------------------------------------------------------------------------------------------
  307.     --CLOSED
  308.     ------------------------------------------------------------------------------------------
  309.     Closed = {
  310.         -- Called when Closed State is Set.
  311.         ----------------------------------------------------------
  312.         OnBeginState = function(self)
  313.             --System.LogToConsole("SERVER:Closed");
  314.             if(self.Timer~=0)then
  315.                 Game:KillTimer(self.Timer);
  316.             end
  317.             self.Timer=Game:SetTimer(self,self.OpeningTime);
  318.  
  319.             self.EventSent = nil;
  320.             self.EndReached = nil;
  321.         end,
  322.         ----------------------------------------------------------
  323.         OnEndState = function(self)
  324.             Game:KillTimer(self.Timer);
  325.             self.Timer=0;
  326.         end,
  327.         ----------------------------------------------------------
  328.         OnContact = function(self, other)
  329.             
  330.             if ( ((self.bLocked==0) and self.Properties.bAutomatic==0) and (not other.ai)) then -- ai can always open the door                
  331.                 
  332.                 -- draw this for the local player/client only
  333.                 if ((other==_localplayer)) then
  334.                     Hud.label=Localize("OpenDoor"); --"Press USE button to open the door";
  335.                 end
  336.                 do return end                
  337.             end
  338.  
  339.             self:OnOpen(self,other,0);
  340.         end,
  341.  
  342.         OnTimer = function( self )
  343.             --System:LogToConsole("No contact");            
  344.         end,
  345.         ----------------------------------------------------------
  346. --        OnTimer = function(self)
  347. --        end,
  348.         ----------------------------------------------------------
  349.         OnEvent = function(self,eid)
  350.             if ((eid==ScriptEvent_OnTimer) and (self.Properties.bUseAnimatedModel==0))
  351.             then
  352.                 self:SetObjectPos(0, g_Vectors.v000);
  353.                 self:SetObjectPos(1, g_Vectors.v000);
  354.             end
  355.         end
  356.         ----------------------------------------------------------
  357.     }
  358. }
  359.  
  360. ------------------------------------------------------------------------------------------
  361. ------------------------------------------------------------------------------------------
  362. -- CLIENT
  363. ------------------------------------------------------------------------------------------
  364. ------------------------------------------------------------------------------------------
  365. AutomaticDoor["Client"]={
  366.     OnInit = AutomaticDoor.OnInit,
  367.     ------------------------------------------------------------------------------------------
  368.     --INACTIVE
  369.     ------------------------------------------------------------------------------------------
  370.     Inactive = {
  371.     },
  372.     ------------------------------------------------------------------------------------------
  373.     --OPENED
  374.     ------------------------------------------------------------------------------------------
  375.     Opened = {
  376.         ----------------------------------------------------------
  377.         -- Called when Opened State is Set.
  378.         OnBeginState = function(self)
  379.             self:StartAnimation(0,"default");
  380.             --System.LogToConsole("CLIENT:Open");
  381.             self:EnableUpdate(1);
  382.             self.numupdates=0;
  383.  
  384.             self:UpdatePortal();            
  385.         end,
  386.         OnEndState = function(self)
  387.             self:EnableUpdate(0);
  388.         end,
  389.         ----------------------------------------------------------
  390.         OnUpdate = function(self, dt)
  391.             self.Distance = self.Distance + dt * self.Properties.MovingSpeed;
  392.             if ( self.Distance > self.Properties.MovingDistance ) then
  393.                 self.Distance = self.Properties.MovingDistance;
  394.                 if ( not self.EventSent ) then
  395.                     self.Event_Opened(self);
  396.                 end
  397.                 self.EventSent = 1;
  398.             end
  399.  
  400.             if (self.Properties.bUseAnimatedModel==0) then
  401.                 self.temp_vec.x = self.Properties.Direction.X * self.Distance;
  402.                 self.temp_vec.y = self.Properties.Direction.Y * self.Distance;
  403.                 self.temp_vec.z = self.Properties.Direction.Z * self.Distance;
  404.                 self:SetObjectPos(0,self.temp_vec);
  405.                 self.temp_vec.x = -(self.Properties.Direction.X * self.Distance);
  406.                 self.temp_vec.y = -(self.Properties.Direction.Y * self.Distance);
  407.                 self.temp_vec.z = -(self.Properties.Direction.Z * self.Distance);
  408.                 self:SetObjectPos(1,self.temp_vec);
  409.             end
  410.  
  411.             --System:LogToConsole("Automatic door client update");
  412.  
  413.             if ((self.numupdates==5) and (self.OpenSound) and (not Sound:IsPlaying(self.OpenSound))) then
  414.                 Sound:SetSoundPosition(self.OpenSound, self:GetPos());
  415.                 --System:LogToConsole("Automatic Door Open("..self:GetPos().x..","..self:GetPos().y..","..self:GetPos().z..",");
  416.                 Sound:PlaySound(self.OpenSound);
  417.             end
  418.  
  419.             self.numupdates=self.numupdates+1;
  420.         end,
  421.  
  422.     },
  423.     ------------------------------------------------------------------------------------------
  424.     --CLOSED
  425.     ------------------------------------------------------------------------------------------
  426.     Closed = {
  427.         -- Called when Closed State is Set.
  428.         ----------------------------------------------------------
  429.         OnBeginState = function(self)
  430.             self:StartAnimation(0,"close");
  431.             --System.LogToConsole("CLIENT:Close");
  432.             --System:LogToConsole("Automatic Door Close("..self:GetPos().x..","..self:GetPos().y..","..self:GetPos().z..",");
  433.             self:EnableUpdate(1);
  434.             self.numupdates=0;
  435.         end,
  436.         OnEndState = function(self)
  437.             self:EnableUpdate(0);
  438.         end,
  439.         ----------------------------------------------------------
  440.         OnUpdate = function(self, dt)
  441.             self.Distance = self.Distance - dt * self.Properties.MovingSpeed;
  442.             if ( self.Distance < 0 ) then
  443.                 self.Distance = 0;
  444.                 if ( not self.EventSent ) then
  445.                     self.Event_Closed(self);
  446.                 end
  447.                 self.EventSent = 1;
  448.                 --self.EndReached = 1;
  449.             end
  450.  
  451.             if (self.Properties.bUseAnimatedModel==0) then
  452.                 local CurrPos = {};
  453.                 self.temp_vec.x = self.Properties.Direction.X * self.Distance;
  454.                 self.temp_vec.y = self.Properties.Direction.Y * self.Distance;
  455.                 self.temp_vec.z = self.Properties.Direction.Z * self.Distance;
  456.                 self:SetObjectPos(0,self.temp_vec);
  457.                 self.temp_vec.x = -(self.Properties.Direction.X * self.Distance);
  458.                 self.temp_vec.y = -(self.Properties.Direction.Y * self.Distance);
  459.                 self.temp_vec.z = -(self.Properties.Direction.Z * self.Distance);
  460.                 self:SetObjectPos(1,self.temp_vec);
  461.             end
  462.  
  463.             if (self.bInitialized==1) then -- [marco] do not play the close sound oninit,onreset,during loading
  464.                 if ((self.numupdates==5) and self.CloseSound and (not Sound:IsPlaying(self.CloseSound)))then
  465.                     Sound:SetSoundPosition(self.CloseSound, self:GetPos());                    
  466.                     Sound:PlaySound(self.CloseSound);
  467.                 end
  468.             end
  469.  
  470.             self.numupdates=self.numupdates+1;
  471.         end,
  472.  
  473.     }
  474. }
  475. ------------------------------------------------------------------------------------------
  476. ------------------------------------------------------------------------------------------
  477.  
  478. function AutomaticDoor:OnShutDown()
  479. end